import {
  Controller,
  Get,
  Post,
  Body,
  Put,
  Delete,
  Param,
  Query,
  Logger,
  HttpException,
  HttpStatus,
  Header,
  UseGuards,
} from "@nestjs/common";
import { RolesService } from "./roles.service";
import { Roles } from "./roles.entity";
import { ConfigService } from "@nestjs/config";
import { QueryRoleDto, CreateRoleDto, UpdateRoleDto } from "./dto";
import { Role } from "../enum/roles.enum";
import { Rolesdc } from "../decorators/role.decorators";
import { RoleGuard } from "../guards/role.guard";
import { JwtGuard } from "../guards/jwt.guard";
import { CaslGuard } from "../guards/casl.guard";
import { Can } from "../decorators/casl.decorator";
import { Action } from "../enum/action.enum";

/**
 * 角色控制器 - 处理角色相关的HTTP请求
 */
@Controller("roles")
// @Rolesdc(Role.ADMIN)
@UseGuards(JwtGuard, RoleGuard, CaslGuard)
@Can(Action.READ, Roles)
export class RolesController {
  private logger = new Logger(RolesController.name);

  // 依赖注入：角色服务和配置服务
  constructor(
    private readonly rolesService: RolesService,
    private readonly configService: ConfigService,
  ) {
    this.logger.log("RolesController initialized");
  }

  /**
   * 获取所有角色（不分页）
   */
  // @Rolesdc(Role.USER)
  @Get("list")
  findAll() {
    return this.rolesService.findAll();
  }

  /**
   * 分页获取角色列表（支持搜索）
   */
  @Get("page")
  getPaginatedRoles(@Query("page") page?: string, @Query("pageSize") pageSize?: string, @Query("name") name?: string) {
    const queryDto: QueryRoleDto = {
      page: page ? parseInt(page, 10) : undefined,
      pageSize: pageSize ? parseInt(pageSize, 10) : undefined,
      name: name || undefined,
    };

    return this.rolesService.getPaginatedRoles(queryDto);
  }

  /**
   * 根据ID查找角色
   * @param id 角色ID
   */
  @Get(":id")
  findOne(@Param("id") id: number) {
    return this.rolesService.findOne(id);
  }

  /**
   * 创建新角色
   * @param role 角色信息
   */
  @Post()
  @Can(Action.CREATE, Roles)
  create(@Body() role: CreateRoleDto) {
    return this.rolesService.create(role);
  }

  /**
   * 更新角色信息
   * @param id 角色ID
   * @param role 更新的角色信息
   */
  @Put(":id")
  @Can(Action.UPDATE, Roles)
  update(@Param("id") id: number, @Body() role: UpdateRoleDto) {
    return this.rolesService.update(id, role);
  }

  /**
   * 删除角色
   * @param id 角色ID
   */
  @Delete(":id")
  @Can(Action.DELETE, Roles)
  remove(@Param("id") id: number) {
    return this.rolesService.remove(id);
  }

  /**
   * 为角色添加用户
   * @param id 角色ID
   * @param userIds 用户ID数组
   */
  @Post(":id/users")
  @Can(Action.UPDATE, Roles)
  addUsersToRole(@Param("id") id: number, @Body("userIds") userIds: number[]) {
    return this.rolesService.addUsersToRole(id, userIds);
  }

  /**
   * 从角色中移除用户
   * @param id 角色ID
   * @param userId 用户ID
   */
  @Delete(":id/users/:userId")
  @Can(Action.DELETE, Roles)
  removeUserFromRole(@Param("id") id: number, @Param("userId") userId: number) {
    return this.rolesService.removeUserFromRole(id - 0, userId - 0);
  }

  /**
   * 获取角色下的所有用户
   * @param id 角色ID
   */
  @Get(":id/users")
  getRoleUsers(@Param("id") id: number) {
    return this.rolesService.getRoleUsers(id);
  }

  /**
   * 设置角色的用户列表（覆盖式更新）
   * @param id 角色ID
   * @param userIds 用户ID数组
   */
  @Put(":id/users")
  @Can(Action.UPDATE, Roles)
  setRoleUsers(@Param("id") id: number, @Body("userIds") userIds: number[]) {
    return this.rolesService.setRoleUsers(id, userIds);
  }
}
